//
//  LTTabBarViewController.swift
//  textweibo
//
//  Created by 李付涛 on 2017/3/16.
//  Copyright © 2017年 江苏岳创信息科技有限公司. All rights reserved.
//

import UIKit

class LTTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setChildControllers()
        
        setupComposeController()
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
    // MARK   监听方法
    func composeStaus(){
        print("dfada")
    }
    
    //撰写按钮
     lazy var composrButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
}
    
    extension LTTabBarViewController {
        
        func setupComposeController() {
            tabBar.addSubview(composrButton)
            
            let count = CGFloat(childViewControllers.count)
            //向内缩进的宽度减小，让按钮的宽度增大，盖住容错点
            let w = tabBar.bounds.width/count-1
            
            composrButton.frame = tabBar.bounds.insetBy(dx: 2*w, dy: 0)
            
            composrButton.addTarget(self, action: #selector(composeStaus), for: .touchUpInside)
            
        }
        
     func setChildControllers(){
        
        let array = [
        ["clsName":"LTHomeViewController","title":"首页","imageName":"home"],
       ["clsName":"LTMessageViewController","title":"消息","imageName":"message_center"],
       ["clsName":"UIViewController"],
        ["clsName":"LTFindViewController","title":"发现","imageName":"discover"],
    ["clsName":"LTMeViewController","title":"我","imageName":"profile"]
            
        ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
        
        }
        
        func controller(dict:[String:String]) -> UIViewController {
            guard let clsName = dict["clsName"],
               let title = dict["title"],
                let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName ) as? UIViewController.Type
                else {
                return UIViewController()
            }
            
            
            let vc = cls.init()
            vc.title = title;
            vc.tabBarItem.image = UIImage(named:"tabbar_" + imageName)
            vc.tabBarItem.selectedImage = UIImage(named:"tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
            //设置tabbar字体颜色
            vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
            //改大小 系统默认12字
            vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: UIControlState(rawValue:0))
            let nav = LTNavigationViewController(rootViewController:vc)
            return nav
        }
        
        
        
        
    }
    
    


